package com.android.newsreader;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
* An activity class that displays a list of
* News items saved as favourites
*/
public class FavsList extends Activity implements OnItemClickListener {

	private RSSItemProvider mDb4oHelper = null;
	private List<RSSItem> mFavourites = null;

	/**
	 *  Called when the activity is first created.
	 *   
	 * */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news);
        
        UpdateDisplay();
	}
	
	/**
 	 *  Updates item list in this activity with items 
 	 *  retrieved from the database
 	 *  
 	 * */
	private void UpdateDisplay()
    {
        TextView feedtitle = (TextView) findViewById(R.id.feedtitle);
        TextView feedpubdate = (TextView) findViewById(R.id.feedpubdate);
        ListView itemlist = (ListView) findViewById(R.id.itemlist);
  
        // Retrieves all RSSItem objects stored in the database
        mFavourites = dbHelper().findAll();
        
        if (mFavourites.size() == 0)
        {
        	feedtitle.setText(getString(R.string.no_favs));
        	return;
        }
        
        feedtitle.setText("Favourites");

        ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,mFavourites);

        itemlist.setAdapter(adapter);
        
        itemlist.setOnItemClickListener(this);
        
        itemlist.setSelection(0);
        
    }
	
	
	
	/**
     * Create RSSItemProvider instance
     */
     private RSSItemProvider dbHelper() {
         if (mDb4oHelper == null) {
            mDb4oHelper = RSSItemProvider.getInstance(this);
            mDb4oHelper.db();
         }
         
         return mDb4oHelper;
     } 
     
     /**
      * Close database before exiting the application
      */
      @Override
      protected void onPause() {
           super.onDestroy();
           if (mDb4oHelper != null)
        	   mDb4oHelper.close();
      }
      
      
      /**
       * Opens database when activity is resumed
       */
      @Override
      protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (mDb4oHelper != null)
     	   this.dbHelper();
		
		UpdateDisplay();
      }

    /**
   	 *  Set behaviour for list item clicks
   	 *  Will open the ShowStory activity for selected item
   	 *  
   	 * */
	public void onItemClick(AdapterView parent, View v, int position, long id)
      { 
     	 
     	 Intent itemintent = new Intent(this,ShowStory.class);
          
     	 Bundle b = new Bundle();
     	 b.putString("title", mFavourites.get(position).getTitle());
     	 b.putString("description", mFavourites.get(position).getDescription());
     	 b.putString("link", mFavourites.get(position).getLink());
     	 b.putString("pubdate", mFavourites.get(position).getPubDate());
     	 b.putInt("source", 3);
     	 
     	 itemintent.putExtra("android.intent.extra.INTENT", b);
          
     	 startActivity(itemintent);
      }

}
